<?php

/**
 * This is the model class for table "planet".
 *
 * The followings are the available columns in table 'planet':
 * @property integer $id
 * @property integer $player_id
 * @property string $created
 * @property string $updated
 * @property integer $status
 * @property string $name
 * @property integer $galaxy
 * @property integer $solar
 * @property integer $position
 * @property integer $points
 *
 * The followings are the available model relations:
 * @property Player $player
 * @property PlanetDetail[] $planetDetails
 */
class Planet extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Planet the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'planet';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, galaxy, solar, position', 'required'),
			array('player_id, status, galaxy, solar, position, points', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>30),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, player_id, created, updated, status, name, galaxy, solar, position, points', 'safe', 'on'=>'search'),
      array('created, updated, status', 'safe'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'player' => array(self::BELONGS_TO, 'Player', 'player_id'),
			'planetDetails' => array(self::HAS_MANY, 'PlanetDetail', 'planet_id'),
		);
	}

  public function behaviors()
  {
    return array(
      'CTimestampBehavior' => array(
        'class' => 'zii.behaviors.CTimestampBehavior',
        'createAttribute' => 'created',
        'updateAttribute' => 'updated',
      )
    );
  }

  public function scopes()
  {
    return array(
      'active' => array(
        'condition' => 'status = 1',
      ),
    );
  }

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'player_id' => 'Player',
			'created' => 'Created',
			'updated' => 'Updated',
			'status' => 'Status',
			'name' => 'Name',
			'galaxy' => 'Galaxy',
			'solar' => 'Solar',
			'position' => 'Position',
			'points' => 'Points',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('player_id',$this->player_id);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('updated',$this->updated,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('galaxy',$this->galaxy);
		$criteria->compare('solar',$this->solar);
		$criteria->compare('position',$this->position);
		$criteria->compare('points',$this->points);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

  /**
   *
   * @param type $playerId The player the planets belong to.
   * @param type $planets Array from actionPublicInfo.
   */
  public function savePlanetsFromPublicInfo($playerId, $planets)
  {
    if (!empty($planets)) {
      foreach ($planets as $planet) {
        $newPlanet = new Planet();
        $newPlanet->player_id = $playerId;
        $newPlanet->name = $planet['name'];
        $newPlanet->points = $planet['points'];
        $newPlanet->galaxy = $planet['coord_galaxy'];
        $newPlanet->solar = $planet['coord_solar'];
        $newPlanet->position = $planet['coord_position'];
        if ($newPlanet->save()) {
          Yii::log('Planet '.$newPlanet->name.' ('.$newPlanet->galaxy.':'.$newPlanet->solar.':'.Parser::planetPosToChr($newPlanet->position).') erstellt', CLogger::LEVEL_TRACE, 'sntools.model.planet');
        }
      }
    } else {
      Yii::log('KEINE Planeten gefunden für Player '.$playerId, CLogger::LEVEL_WARNING, 'sntools.model.planet');
    }
  }
}